<template>
  <el-card class="tableCard">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="门名称" prop="doorName">
        <el-input
          v-model="queryParams.doorName"
          placeholder="请输入门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地理位置" prop="areaId">
        <treeselect
          style="width:200px"
          v-model="queryParams.areaId"
          :options="areaOptions"
          :normalizer="normalizer"
          @select="handleTreeSelectQuery"
          placeholder="请选择地理位置"
        />
      </el-form-item>
      <!-- <el-form-item label="门所属区域" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入门所属区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="设备编号" prop="deviceId">
        <el-select
          v-model="queryParams.deviceId"
          placeholder="请选择设备编号"
          clearable
          size="small"
        >
          <el-option
            :label="item.deviceName"
            :value="item.deviceId"
            v-for="(item, index) in deviceList"
            :key="index"
            @click.native="handleSelectQuery(item.deviceName)"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="mark">
        <el-input
          v-model="queryParams.mark"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        class="right-toolbar"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="doorList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门编号" align="center" prop="doorId" />
      <el-table-column label="门名称" align="center" prop="doorName" />
      <!-- <el-table-column label="地理位置" align="center" prop="areaId" /> -->
      <el-table-column label="门所属区域" align="center" prop="areaName" />
      <!-- <el-table-column label="设备编号" align="center" prop="deviceId" /> -->
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改door对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-form-item label="门名称" prop="doorName">
          <el-input v-model="form.doorName" placeholder="请输入门名称" />
        </el-form-item>
        <el-form-item label="地理位置" prop="areaId">
          <!-- <el-input v-model="form.areaId" placeholder="请输入地理位置" /> -->
          <treeselect
            v-model="form.areaId"
            :options="areaOptions"
            :normalizer="normalizer"
            @select="handleTreeSelectForm"
            placeholder="请选择地理位置"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceId">
          <!-- <el-input v-model="form.deviceId" placeholder="请输入设备编号" /> -->
          <el-select
            v-model="form.deviceId"
            placeholder="请选择设备编号"
            clearable
            size="small"
          >
            <el-option
              :label="item.deviceName"
              :value="item.deviceId"
              v-for="(item, index) in deviceList"
              :key="index"
              @click.native="handleSelectForm(item.deviceName)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input
            type="textarea"
            v-model="form.mark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  listDoor,
  getDoor,
  delDoor,
  addDoor,
  updateDoor,
  exportDoor,
} from "@/api/door";

import { listDevice } from "@/api/device";
import { listArea } from "@/api/area";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Door",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDmsDevice: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // door表格数据
      doorList: [],
      // dms表格数据
      dmsDeviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doorName: null,
        areaId: null,
        areaName: null,
        deviceId: null,
        deviceName: null,
        mark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 地理位置列表
      areaList: [],
      // 设备列表
      deviceList: [],
      // 地理位置选项
      areaOptions: undefined,
    };
  },
  beforeCreate() {
    listArea().then((res) => {
      this.areaList = res.data.data;
      this.areaOptions = [];
      const data = { areaId: 0, areaName: "顶级节点", children: [] };
      data.children = this.handleTree(res.data.data, "areaId", "parentId");
      this.areaOptions.push(data);
      console.log(this.areaOptions);
    });
    listDevice().then((res) => {
      this.deviceList = res.data.rows;
    });
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectForm(value) {
      this.form.deviceName = value;
    },
    /** 处理树性下拉选择框 */
    handleTreeSelectForm(value) {
      this.form.areaName = value.areaName;
    },
    handleSelectQuery(value) {
      this.queryParams.deviceName = value;
    },
    /** 处理树性下拉选择框 */
    handleTreeSelectQuery(value) {
      this.queryParams.areaName = value.areaName;
    },
    /** 转换area数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaName,
        children: node.children,
      };
    },
    /** 查询door列表 */
    getList() {
      this.loading = true;
      listDoor(this.queryParams).then((response) => {
        this.doorList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doorId: null,
        doorName: null,
        areaId: null,
        areaName: null,
        deviceId: null,
        deviceName: null,
        mark: null,
      };
      this.dmsDeviceList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.areaName = null
      this.queryParams.deviceName = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.doorId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加door";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const doorId = row.doorId || this.ids;
      getDoor(doorId).then((response) => {
        this.form = response.data.data;
        this.dmsDeviceList = response.data.dmsDeviceList;
        this.open = true;
        this.title = "修改door";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.dmsDeviceList = this.dmsDeviceList;
          if (this.form.doorId != null) {
            updateDoor(this.form).then((response) => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoor(this.form).then((response) => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const doorIds = row.doorId || this.ids;
      this.$confirm(
        '是否确认删除door编号为"' + doorIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function() {
          return delDoor(doorIds);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        });
    },
    /** dms序号 */
    rowDmsDeviceIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** dms添加按钮操作 */
    handleAddDmsDevice() {
      let obj = {};
      obj.deviceName = "";
      obj.deviceIp = "";
      obj.deviceMac = "";
      obj.deviceHeartbeat = "";
      obj.onlineDate = "";
      obj.faceCount = "";
      obj.faceImage = "";
      obj.version = "";
      this.dmsDeviceList.push(obj);
    },
    /** dms删除按钮操作 */
    handleDeleteDmsDevice() {
      if (this.checkedDmsDevice.length == 0) {
        this.$alert("请先选择要删除的dms数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.dmsDeviceList.splice(this.checkedDmsDevice[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleDmsDeviceSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.dmsDevice.clearSelection();
        this.$refs.dmsDevice.toggleRowSelection(selection.pop());
      } else {
        this.checkedDmsDevice = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {},
  },
};
</script>
<style>
.vue-treeselect__control {
  margin-top: 4px;
  height: 32px;
}
.vue-treeselect__input-container {
  height: 5px !important;
}
</style>
